package hibernate.hql;


import com.txm.model.hibernate.ClassRoom;
import com.txm.model.hibernate.Hibernate;
import com.txm.model.hibernate.IDCard;
import com.txm.model.hibernate.User;
import com.txm.model.hql.Order;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;


public class OrderTest {
    private Session session = null;

    @Test
    public void testWhere1() {
        String hql = " from Order o where o.orderItems is not empty ";
        Query query = session.createQuery(hql);
        List<Order> orders = query.list();

        for (Order order : orders) {
            System.out.println(order.getCustomer().getName());
            System.out.println(order.getAmount());
            System.out.println(order.getTradeDate());
        }
    }

    @Before
    public void setUp() throws Exception {
        //创建hibernate配置对象
        Configuration configuration = new Configuration().configure();
        configuration.addAnnotatedClass(Hibernate.class).addAnnotatedClass(IDCard.class).addAnnotatedClass(User.class).addAnnotatedClass(ClassRoom.class);
        //创建服务注册对象
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
        //生成sessionfactory
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        session = sessionFactory.getCurrentSession();
    }

    @After
    public void tearDown() throws Exception {
        session.close();
    }

}
